package com.android.presenter.info;

import android.content.Context;

import com.android.model.entity.res.CreateTicketResult;
import com.android.model.info.LocalSp;
import com.android.model.info.UserModel;
import com.android.model.net.HttpObserver;
import com.android.presenter.base.BasePresenter;
import com.android.presenter.iview.ICreateTicketView;
import com.android.util.log.LogUtil;

/**
 * author:  TenFace
 * date:    2017/9/27
 * description: 创建 order ticket
 */

public class CreateTicketPresenter extends BasePresenter<ICreateTicketView> {

    private static final String TAG = CreateTicketPresenter.class.getSimpleName();
    private UserModel mUserModel;

    public CreateTicketPresenter(ICreateTicketView iCreateTicketView) {
        super(iCreateTicketView);
        mUserModel = UserModel.getInstance();
    }

    public void createTicket(final Context context,
                             String accountId,
                             String session) {

        mUserModel.createTicket(accountId, session, new HttpObserver<CreateTicketResult>() {
            @Override
            public void onSuccess(CreateTicketResult createTicketResult) {
                LogUtil.e(TAG, "成功！");
                if (mIView != null) {
                    mIView.createTicketSuccess(createTicketResult);
                    LocalSp.getInstance(context).saveOrderTradeTicket(createTicketResult.getValue());
                }
            }

            @Override
            public void onError(int errType, String errMessage) {
                LogUtil.e(TAG, "失败码：" + errType + "》》信息：" + errMessage);
                if (mIView != null) {
                    mIView.createTicketFail(errType, errMessage);
                }
            }
        }, mIView.getLifeSubject());
    }

}
